package com.atguigu.lease.web.admin.custom.interceptors;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.jwts.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取request中请求头的token(access_token)
        String accessToken = request.getHeader("access_token");
        //2.检查token是否为null
        if(ObjectUtils.isEmpty(accessToken)){
            //没有token没有登录
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        //3.检查token是否过期和有效
        //内部自动抛异常
        JwtUtil.parseToken(accessToken);
        //true放行  | false拦截 [!(preHandler()) return;]
        return true;
    }

}
